package moduloRemoto.bo;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import moduloRemoto.bo.interfaces.AvisoRemote;
import moduloRemoto.dao.interfaces.ServerLocal;
import moduloRemoto.pojo.Aviso;

import org.jboss.annotation.ejb.RemoteBinding;

@Stateless
@Remote(AvisoRemote.class)
@RemoteBinding(jndiBinding="ejb/aviso")
public class AvisoBO implements AvisoRemote
{
	@EJB 
	private ServerLocal dao;

	@Override
	public Aviso persistir(Aviso objAvi)
	{
		if (objAvi == null)
			return null;

		try
		{
			if (objAvi.getIdAvi() > 0)
				objAvi = dao.update(objAvi);
			else
				objAvi = dao.insert(objAvi);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}

		return objAvi;
	}

	@Override
	public Boolean excluir(int idAvi)
	{
		if (idAvi <= 0)
			return false;
		
		try
		{
			Aviso objAvi = this.carregarObjeto(idAvi);

			if (objAvi != null)
				dao.remove(objAvi);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	@Override
	public Aviso carregarObjeto(int idAvi)
	{
		if (idAvi <= 0)
			return null;
		
		Aviso objAvi = null;
		
		try
		{
			objAvi = dao.find(Aviso.class, idAvi);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		return objAvi;
	}

	@Override
	public List<Aviso> getAvisosByProjeto(int idPro)
	{
		List<Aviso> listaAvisos = new ArrayList<Aviso>();
		
		try
		{
			for (Object objeto : dao.executeQuery("SELECT a FROM Aviso a WHERE a.projetoAvi = " + idPro))
				listaAvisos.add((Aviso)objeto);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		return listaAvisos;
	}
}